﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Code;
using DB;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Mvc;
using Models;

namespace LayuiAdmin.Controllers {
    [Authorize]
    public class OrderController : Controller {
        public IActionResult ProductInfo() {
            Notice notice = new Notice();
            ProductInfo product = new ProductInfo();
            Users users = new Users();

            using (var context = new SqlServerDB()) {
                long id = 0;
                string strId = HttpContext.User.Identity.GetValue(ClaimTypes.Sid);
                long.TryParse(strId, out id);

                users = context.Users.FirstOrDefault(m => m.Id == id);

                context.Entry(users).Collection(u => u.ShippingAddress).Query().ToList();

                var address = users.ShippingAddress.FirstOrDefault();

                ViewData["user"] = users;

                ViewBag.IsSetAddress = address != null;

                product = context.Products.FirstOrDefault();
            }

            return View(product);
        }

        public IActionResult BuyProduct(decimal price, int qty) {
            Notice notice = new Notice();
            Users users = new Users();
            using (var context = new SqlServerDB()) {
                ViewBag.Price = price;
                ViewBag.Qty = qty;
                ViewBag.Amount = Math.Round(price * qty, 2);

                var product = context.Products.FirstOrDefault();

                if (product != null) {
                    ViewBag.Product = product.Name;
                } else {
                    ViewBag.Product = "";
                }

                long id = 0;
                string strId = HttpContext.User.Identity.GetValue(ClaimTypes.Sid);
                long.TryParse(strId, out id);

                users = context.Users.FirstOrDefault(m => m.Id == id);

                context.Entry(users).Collection(u => u.ShippingAddress).Query().ToList();

                var address = users.ShippingAddress.FirstOrDefault();

                ViewData["user"] = users;

                if (address != null) {
                    CacheHelper cache = new CacheHelper();
                    var areaList = cache.GetAreaList();
                    var province = areaList.FirstOrDefault(m => m.id == address.ProvinceId);
                    var city = areaList.FirstOrDefault(m => m.id == address.CityId);
                    var area = areaList.FirstOrDefault(m => m.id == address.AreaId);

                    ViewData["detail"] = $"{ (province == null ? "" : $"{province.name} ")}{(city == null ? "" : $"{city.name} ")}{(area == null ? "" : $"{area.name} ")}{address.Address}";
                }

                notice = context.Notices.FirstOrDefault(m => m.NoticeType == NoticeType.购买协议);

                var setting = context.Settings.Where(m => m.SystemType == SystemType.收款信息).ToList();

                ViewData["setting"] = setting;

                ViewData["notice"] = notice;

                List<ItemList> cashTypes = BaseCode.GetEnumByList<CashType>();
                ViewBag.Cash = cashTypes;
            }

            return View();
        }

        public IActionResult List() {
            List<ItemList> list = BaseCode.GetEnumByList<OrderStatus>(new ItemList { text = "请选择", value = 0 });
            UserTypes userTypes;
            Enum.TryParse(HttpContext.User.Identity.GetValue("UserType"), out userTypes);

            ViewData["userType"] = (int)userTypes;

            if (userTypes == UserTypes.普通用户) {
                ViewData["Id"] = HttpContext.User.Identity.GetValue(ClaimTypes.Sid);
            } else {
                ViewData["Id"] = 0;
            }

            List<ItemList> cashTypes = BaseCode.GetEnumByList<CashType>();
            ViewBag.Cash = cashTypes;

            using (var context = new SqlServerDB()) {
                var setting = context.Settings.Where(m => m.SystemType == SystemType.收款信息).ToList();

                ViewData["setting"] = setting;
            }

            return View(list);
        }

        public IActionResult Remittance(long id) {
            using (var context = new SqlServerDB()) {
                List<ItemList> cashTypes = BaseCode.GetEnumByList<CashType>();
                ViewBag.Cash = cashTypes;

                OrderInfos order = context.Orders.FirstOrDefault(m => m.Id == id);
                ViewData["Id"] = HttpContext.User.Identity.GetValue(ClaimTypes.Sid);

                var setting = context.Settings.Where(m => m.SystemType == SystemType.收款信息).ToList();

                ViewData["setting"] = setting;

                return View(order);
            }
        }
    }
}